<?php
/**
 * 《邮箱信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Zone.class.php 804 2009-12-30 10:05:41Z sunxy $
 */
require_once(dirname(__FILE__) . '/Info.class.php');

class Vpopmail extends Info {

	public $tbl = Constant::tbl_vpopmail_vpopmail;

	public $orderby = array(
		'-' => '请选择',
		'pw_name' => '邮箱',
		'pw_domain' => '域名',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
		
		$this->dsn = 'vpopmail';
		$this->dsn_r = 'vpopmail';
		$this->dsn_w = 'vpopmail';
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'邮箱信息' => array('cc'=>'emp.php?do=Vpopmail.find', 'js'=>''),
			'邮箱使用' => array('cc'=>'log.php?do=Vlog.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$province = $this->code_option('4');
		$controller->assign_by_ref('province_option', $province);

		$controller->display($request, 'emp/vpopmail/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['pw_name'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '1') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = date("Y-m-d H:s", $row['timestamp']);
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$name_s = $request['name_s'];
		$domain_s = $request['domain_s'];

		$where = " WHERE pw_domain = 'tecmold.com'";

		if (!empty($name_s) && $name_s <> '-') {
			$where .= " AND pw_name = '" . $name_s . "'";
		}

		if (!empty($domain_s)) {
			$where .= " AND pw_domain = '" . $domain_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/vpopmail/list');
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
		
		// debug(print_r($row, true));
		
		require_once 'EmpJob.class.php';
		
		$aa = new EmpJob();
		
		$row['dd'] = $aa->_leave_dd($row['emp']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/vpopmail/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['email'] = $row['pw_name'] . "@" . $row['pw_domain'];
		$row['e'] = $this->_emp_email($row['email']);
		
		$row['emp'] = $row['e']['id'];
				
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _emp_email($email) {
		require_once(dirname(__FILE__) . '/Emp.class.php');
		$aa = new Emp();
		
		return $aa->_emp_email($email);
	}

	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['city']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function ns13($controller, $request) {
		$a = $request['_tec'];
		
		$dir = $this->_tec_r($a, 3, "/home/b/file/公共");
		$day = $this->_tec_r($a, 4, 3);
		
		$this->_pub($dir, 0, "-$day day");

		logdate($controller, $request);
		return true;
	}
	
	function sj13($controller, $request) {
		$a = $request['_tec'];
	
		$dir = $this->_tec_r($a, 3, "/home/file/公共");
		$day = $this->_tec_r($a, 4, 3);
	
		$this->_pub($dir, 0, "-$day day");
	
		logdate($controller, $request);
		return true;
	}
	
	function qx13($controller, $request) {
		$a = $request['_tec'];
	
		$dir = $this->_tec_r($a, 3, "/opt/b/file/公共");
		$day = $this->_tec_r($a, 4, 3);
	
		$this->_pub($dir, 0, "-$day day");
	
		logdate($controller, $request);
		return true;
	}
	
	function _tec_r($a, $k, $default) {
		if (count($a) > $k) {
			return $a[$k];
		}
			
		return $default;
	}
	
	function _pub($dir, $c, $day) {
		$c++;
		
		$dh = opendir($dir);
		$ret = 0;
		while (($file = readdir($dh)) !== false) {
			if ($file == '.' || $file == '..') {
				continue;
			}

			$fmt = "%s/%s";
			$s = sprintf($fmt, $dir, $file);
			if (is_dir($s)) {
				$ret += $this->_pub($s, $c, $day);
			} else {
				$a = lstat($s);
				if ($a['ctime'] < strtotime($day)) {
					echo sprintf("%s delete...\n", $s);
					unlink($s);
				} else {
					$ret++;
				}
			}
		}
		
		if ($c > 1 && $ret == 0) {
			echo sprintf("%s delete dir...\n", $dir);
			rmdir($dir);
		}

		closedir($dh);
		
		return $ret;
	}

	function ftp($controller, $request) {
		$dir = "/home/ftp/tecmold.com/ftp";
		$this->_ftp($dir, "-3 day");

		logdate($controller, $request);
		return true;
	}

	function _ftp($dir, $day) {
		$dh = opendir($dir);
		while (($file = readdir($dh)) !== false) {
			if ($file == '.' || $file == '..') {
				continue;
			}

			$fmt = "%s/%s";
			$s = sprintf($fmt, $dir, $file);
			if (is_dir($s)) {
				$this->_ftp($s, $day);
			} else {
				$a = lstat($s);
				if ($a['ctime'] < strtotime($day)) {
					echo sprintf("%s delete...\n", $s);
					unlink($s);
				}
			}
		}

		closedir($dh);
	}

	function file($controller, $request) {
		$dir = "/home/file";
		// $dir = "/home/sunxy/file";
		$this->_file($dir, "file");

		logdate($controller, $request);
		return true;
	}

	function _file($dir, $up) {
		$dh = opendir($dir);
		while (($file = readdir($dh)) !== false) {
			if ($file == '.' || $file == '..') {
				continue;
			}
				
			$fmt = "%s/%s";
			$s = sprintf($fmt, $dir, $file);
			if (is_dir($s)) {
				$this->_file($s, $file);
			} else {
				$a = lstat($s);

				if (preg_match("/(\.exe)$/i", $file)) {
					if (preg_match("/^$up/i", $file)) {
						echo date("Y-m-d H:i:s", $a['mtime']) . "\n";
						echo sprintf("%s delete...\n", $s);
						unlink($s);
					}
				}

				if (preg_match("/(desktop\.ini|folder\.htt)$/i", $file)) {
					echo sprintf("%s delete...\n", $s);
					unlink($s);
				}
			}
		}

		closedir($dh);
	}

	function t($controller, $request) {
		require_once 'Mail/mimeDecode.php';

		$dir = "/home/vpopmail/domains/winhap.net/log/Maildir";
		// $dir = "/home/sunxy/test/Maildir";
		$this->_mail($dir);

		logdate($controller, $request);
		return true;
	}

	function _mail($dir) {
		$dh = opendir($dir);
		while (($file = readdir($dh)) !== false) {
			if ($file == '.' || $file == '..') {
				continue;
			}

			// echo $file . "\n";

			$fmt = "%s/%s";
			$s = sprintf($fmt, $dir, $file);
			if (is_dir($s)) {
				$this->_mail($s);
			} else {
				if (!preg_match("/^(\d+)/i", $file)) {
					continue;
				}
				$this->_eml($s);
			}
		}

		closedir($dh);
	}

	function _eml($file) {
		if (!preg_match("/(new|cur|tmp)/i", $file)) {
			return false;
		}

		$a = lstat($file);
		if ($a['ctime'] < strtotime("-20 minute")) {
			if ($a['ctime'] < strtotime("-7 day")) {
				unlink($file);
			}
			return false;
		}

		if (intval($a['size']) > 1024*1024*2) {
			unlink($file);
			return false;
		}
/*
		$s = file_get_contents($file);
		if ($this->_spam($s)) {
			echo $file . " D\n";
			unlink($file);
		} else {
			echo $file . " S\n";
		}
*/
		return true;
	}

	function _spam($input) {
		$params['include_bodies'] = false;
		$params['decode_headers'] = true;
		$params['input'] = $input;

		$structure = Mail_mimeDecode::decode($params);

		// print_r($structure->headers);

		if (preg_match("/\*\*\*\*\*SPAM\*\*\*\*\*/i", $structure->headers['subject'])) {
			return true;
		}

		if (isset($structure->headers['x-spam-status'])) {
			// echo $structure->headers['x-spam-status'] . "\n";
			if ($this->_spam_status($structure->headers['x-spam-status'])) {
				return true;
			}
		}

		if (preg_match("/tecmold\.com/i", $structure->headers['from'])) {
			return false;
		}

		if (preg_match("/tecmold\.com/i", $structure->headers['to'])) {
			return false;
		}

		return false;
		// return true;
	}

	function _spam_status($status) {
		$a = preg_split ("/[\,|\s]+/i", $status, 3);
		$pos = strrpos($a[1], "=");
		if ($pos === false) {
			return false;
		}

		$score = substr($a[1], $pos + 1);
		if (floatval($score) > 1) {
			return true;
		}

		return false;
	}

	function b($controller, $request) {
		$s = "*****SPAM*****test";

		if (preg_match("/\*\*\*\*\*SPAM\*\*\*\*\*/", $s)) {
			echo "OK";
		}

		logdate($controller, $request);
		return true;
	}
}
?>
